/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.dtseditor;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
 *
 * @author yura
 */
public class Util {

    ////////////////////////////////////////////////////////////////////////
    //  IO Utils
    ////////////////////////////////////////////////////////////////////////

    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public static byte[] read(File file) throws IOException {
        return toByteArray(new FileInputStream(file));
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copy(input, output);
        return output.toByteArray();
    }

    public static void copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
    }

    ////////////////////////////////////////////////////////////////////////
    //  XML Utils
    ////////////////////////////////////////////////////////////////////////


    public static byte[] formatXml(byte[] data)
            throws TransformerConfigurationException, TransformerException{

        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setParameter(OutputKeys.INDENT, "yes");
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(data);

        t.transform(new StreamSource(in), new StreamResult(out));
        
        return out.toByteArray();
    }

    ////////////////////////////////////////////////////////////////////////
    //  GUI Utils
    ////////////////////////////////////////////////////////////////////////

    static final RolloverMouseListener MOUSE_LISTENER = new RolloverMouseListener();

    public static void niceEx(Throwable ex, String title, String additionalTxt){
        String step = "";
        int i = 0;
        StringBuilder sb = new StringBuilder().append(additionalTxt);
        for (char ch : String.valueOf(ex).toCharArray()) {
            if (ch == ';'){
                sb.append(";\n").append(step);
                i = 0;
            } else if (ch == ':'){
                step += "   ";
                sb.append(":\n").append(step);
                i = 0;
            } else if ((Character.isWhitespace(ch) || ch == '.') && i >= 80){
                sb.append("\n     ").append(step).append(ch);
                i = 0;
            } else {
                sb.append(ch);
            }
            i ++;
        }

        final JTextPane ta = new JTextPane();
        ta.setText(sb.toString());
        ta.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
        ta.setSize(400, 300);
        ta.setEditable(false);
        JOptionPane.showMessageDialog(null, new JScrollPane(ta), title,
                JOptionPane.ERROR_MESSAGE);
    }

    public static void niceEx(Throwable ex, String title){
        niceEx(ex, title, "");
    }

    public static JButton initBtn(JButton button){
        button.setHideActionText(false);

        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.addMouseListener(MOUSE_LISTENER);

        button.setFocusPainted(true);
        return button;
    }

    static class RolloverMouseListener extends MouseAdapter {

        @Override
        public void mouseEntered(MouseEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) source;
                button.setContentAreaFilled(true);
                button.setBorderPainted(true);
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) source;
                button.setContentAreaFilled(false);
                button.setBorderPainted(false);
            }
        }
    }
}
